using System;
using System.IO;
using SerbinAV.MobileSales.Sales.DAL;
using SerbinAV.MobileSales.Sales.Presenters;

namespace SerbinAV.MobileSales.Sales.Common
{
    public class Work:IWork
    {
        private ISaveLoader<Settings> _settingsSerializer;
        private FileInfo _file;
        private Settings _settings;
        private IPresenterFactory _presenterFactory;
        private IDataManager _dataManager;
        private IHandlerException _handlerException;

        private static Work _instance = null;

        protected Work()
        {
            
        }

        public static Work GetInstance ()
        {
            if (_instance == null)
                _instance = new Work();
            return _instance;
        }
    
        public static void Dispose()
        {
            _instance = null;
        }

        public Settings GetSettings()
        {
            Exception ex = null;
            return GetSettings(out ex);
        }

        public Settings GetSettings(out Exception exception)
        {
            exception = null;
            if (_settings == null)
            {
                try
                {
                    using(Stream fs = new FileStream(_file.FullName, FileMode.Open, FileAccess.Read,FileShare.Read))
                    {
                        _settings = _settingsSerializer.Load(fs);
                    }
                    if (_settings == null)
                    {
                        throw new NullReferenceException();
                    }
                }
                catch (Exception ex)
                {
                    _settings = new Settings();
                    exception = ex;
                }
            }
            
            return _settings;
        }

        public void SaveSettings()
        {
            if (_settings == null)
            {
                return;
            }
            using (Stream fs = new FileStream(_file.FullName, FileMode.Create))
            {
                _settingsSerializer.Save(_settings, fs);
            }
        }

        public IDataManager DataManager{
            get
            {
                return _dataManager;
            }
            set
            {
                _dataManager = value;
            }
        }

        public ISaveLoader<Settings> SettingsSerializer
        {
            set
            {
                _settingsSerializer = value;
            }
            get
            {
                return _settingsSerializer;
            }
        }

        public FileInfo File
        {
            get
            {
                return _file;
            }
            set
            {
                _file = value;
            }
        }

        #region IWork Members

        public IPresenterFactory PresenterFactory
        {
            get
            {
                return _presenterFactory;
            }
            set
            {
                _presenterFactory = value;
            }
        }

        #endregion

        public IHandlerException HandlerException
        {
            get
            {
                return _handlerException;
            }
            set
            {
                _handlerException = value;
            }
        }

    }
}